package study.become

import akka.actor.SupervisorStrategy.Restart
import akka.actor.{Actor, ActorRef, ActorSystem, OneForOneStrategy, Props, SupervisorStrategy}

import scala.concurrent.duration._

/**
 * @Description: context.become 的使用
 * @Author: DJJ
 * @Date: 2019/12/12 16:26
 */
class TaskManager extends Actor{

  override def preStart(): Unit = {
    /**
     *  使用become 原先的receive的方法失效
     */
    context.become(hostKeeper)
  }
  override def receive: Receive = {
    case "hello"=>println("hello")
  }

  def hostKeeper:Receive={
    case "one"=>println("one")
    case "two"=>context.become(taskDistributor)
  }
  def taskDistributor:Receive={
    case "three"=>println("three")
  }

}

object Test extends App{
  private val shoppingCart = ActorSystem("factory")
  private val task: ActorRef = shoppingCart.actorOf(Props[TaskManager],"TaskManager")
  task ! "one"
  task ! "two"
  /**
   *  在发送two 的使用将hostKeeper 变成了taskDistributor 不在接收 one 和 two的消息
   *  使用 unbecome()可以替换上一个actor对象的行为
   */
  task ! "three"
  task ! "hello"
  task ! "one"
  task ! "three"
}
